<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
      
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="订单表的订单ID">
          <a-input placeholder="请输入订单表的订单ID" v-decorator="['orderId', validatorRules.orderId ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="商品表的商品ID">
          <a-input placeholder="请输入商品表的商品ID" v-decorator="['goodsId', validatorRules.goodsId ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="商家订单表的订单ID">
          <a-input placeholder="请输入商家订单表的订单ID" v-decorator="['sellerId', validatorRules.sellerId ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="订单状态">
          <a-input placeholder="请输入订单状态" v-decorator="['orderStatus', validatorRules.orderStatus ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="商品名称">
          <a-input placeholder="请输入商品名称" v-decorator="['goodsName', validatorRules.goodsName ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="商品编号">
          <a-input placeholder="请输入商品编号" v-decorator="['goodsSn', validatorRules.goodsSn ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="商品货品表的货品ID">
          <a-input placeholder="请输入商品货品表的货品ID" v-decorator="['productId', validatorRules.productId ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="商品货品的购买数量">
          <a-input placeholder="请输入商品货品的购买数量" v-decorator="['number', validatorRules.number ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="商品货品的售价">
          <a-input-number v-decorator="[ 'price', validatorRules.price ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="商品总费用">
          <a-input-number v-decorator="[ 'goodsPrice', validatorRules.goodsPrice ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="用户积分减免">
          <a-input-number v-decorator="[ 'integralPrice', validatorRules.integralPrice ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="配送费用">
          <a-input-number v-decorator="[ 'freightPrice', validatorRules.freightPrice ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="优惠券减免">
          <a-input-number v-decorator="[ 'couponPrice', validatorRules.couponPrice ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="团购优惠价减免">
          <a-input-number v-decorator="[ 'grouponPrice', validatorRules.grouponPrice ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="订单费用， = goods_price + freight_price - coupon_price">
          <a-input-number v-decorator="[ 'orderPrice', validatorRules.orderPrice ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="实付费用， = order_price - integral_price">
          <a-input-number v-decorator="[ 'actualPrice', validatorRules.actualPrice ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="商品货品的规格列表">
          <a-input placeholder="请输入商品货品的规格列表" v-decorator="['specifications', validatorRules.specifications ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="商品货品图片或者商品图片">
          <a-input placeholder="请输入商品货品图片或者商品图片" v-decorator="['picUrl', validatorRules.picUrl ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="订单商品评论，如果是-1，则超期不能评价；如果是0，则可以评价；如果其他值，则是comment表里面的评论ID。">
          <a-input-number v-decorator="[ 'comment', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="删除状态(0-正常,1-已删除)">
          <a-input placeholder="请输入删除状态(0-正常,1-已删除)" v-decorator="['delFlag', {}]" />
        </a-form-item>
		
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"

  export default {
    name: "OrderGoodsModal",
    data () {
      return {
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        orderId:{rules: [{ required: true, message: '请输入订单表的订单ID!' }]},
        goodsId:{rules: [{ required: true, message: '请输入商品表的商品ID!' }]},
        sellerId:{rules: [{ required: true, message: '请输入商家订单表的订单ID!' }]},
        orderStatus:{rules: [{ required: true, message: '请输入订单状态!' }]},
        goodsName:{rules: [{ required: true, message: '请输入商品名称!' }]},
        goodsSn:{rules: [{ required: true, message: '请输入商品编号!' }]},
        productId:{rules: [{ required: true, message: '请输入商品货品表的货品ID!' }]},
        number:{rules: [{ required: true, message: '请输入商品货品的购买数量!' }]},
        price:{rules: [{ required: true, message: '请输入商品货品的售价!' }]},
        goodsPrice:{rules: [{ required: true, message: '请输入商品总费用!' }]},
        integralPrice:{rules: [{ required: true, message: '请输入用户积分减免!' }]},
        freightPrice:{rules: [{ required: true, message: '请输入配送费用!' }]},
        couponPrice:{rules: [{ required: true, message: '请输入优惠券减免!' }]},
        grouponPrice:{rules: [{ required: true, message: '请输入团购优惠价减免!' }]},
        orderPrice:{rules: [{ required: true, message: '请输入订单费用， = goods_price + freight_price - coupon_price!' }]},
        actualPrice:{rules: [{ required: true, message: '请输入实付费用， = order_price - integral_price!' }]},
        specifications:{rules: [{ required: true, message: '请输入商品货品的规格列表!' }]},
        picUrl:{rules: [{ required: true, message: '请输入商品货品图片或者商品图片!' }]},
        },
        url: {
          add: "/order/orderGoods/add",
          edit: "/order/orderGoods/edit",
        },
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'orderId','goodsId','sellerId','orderStatus','goodsName','goodsSn','productId','number','price','goodsPrice','integralPrice','freightPrice','couponPrice','grouponPrice','orderPrice','actualPrice','specifications','picUrl','comment','delFlag'))
		  //时间格式化
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            
            console.log(formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>